package dk.pun.aot.client.view;

import com.google.gwt.core.client.GWT;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.uibinder.client.UiBinder;
import com.google.gwt.uibinder.client.UiField;
import com.google.gwt.uibinder.client.UiHandler;
import com.google.gwt.uibinder.client.UiTemplate;
import com.google.gwt.user.client.Command;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.ui.Anchor;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.DecoratedTabBar;
import com.google.gwt.user.client.ui.HasWidgets;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.MenuItem;
import com.google.gwt.user.client.ui.SimplePanel;
import com.google.gwt.user.client.ui.TabBar.Tab;
import com.google.gwt.user.client.ui.Widget;

import dk.pun.aot.client.AnonymousLogin;
import dk.pun.aot.client.widget.AboutDialog;
import dk.pun.aot.client.widget.AdministratorDialog;
import dk.pun.aot.shared.character.CharacterId;
import dk.pun.aot.shared.user.LoginInfo;

public class MainViewImpl extends Composite implements MainView {

	private static MainViewUiBinder uiBinder = GWT.create(MainViewUiBinder.class);

	@UiTemplate("MainView.ui.xml")
	interface MainViewUiBinder extends UiBinder<Widget, MainViewImpl> {}

	@UiField Label message;
	@UiField Anchor selectedCharacter;
	@UiField Label userNickname;
	@UiField Anchor signOutLink;
	@UiField DecoratedTabBar tabBar;
	@UiField MenuItem gmMenu;
	@UiField MenuItem adminMenu;
	@UiField SimplePanel centerPanel;

	private Presenter presenter;
	private String logoutURL;
	private boolean anonymous;

	public MainViewImpl(LoginInfo loginInfo) {
		initWidget(uiBinder.createAndBindUi(this));
		
		this.userNickname.setText(loginInfo.getUserData().getNickname());
		this.logoutURL = loginInfo.getLogoutURL();
		this.anonymous = loginInfo.isAnonymous();
		
		tabBar.addTab("View Character");
		tabBar.addTab("Combat");
		tabBar.addTab("Modify Character");

		Tab viewCharacterTab = tabBar.getTab(0);
		viewCharacterTab.addClickHandler(new ClickHandler () {
			public void onClick(ClickEvent event) {
				presenter.onViewCharacter();
			}
		});

		selectedCharacter.addClickHandler(new ClickHandler () {
			public void onClick(ClickEvent event) {
				presenter.onSelectCharacter();
			}
		});
		
		gmMenu.setCommand(new Command() {
			
			@Override
			public void execute() {
			}
		});
		if (!loginInfo.isUserAdmin()) {
			adminMenu.setVisible(false);
		}
		adminMenu.setCommand(new Command() {
			
			@Override
			public void execute() {
				AdministratorDialog administratorDialog = new AdministratorDialog(presenter, message.getText());
				administratorDialog.show();
				administratorDialog.center();
			}
		});
	}

	@Override
	public void setTabFocus(MainViewTab tab) {
		if (tab != null) {
			switch (tab) {
			case VIEW_CHARACTER:
				this.tabBar.selectTab(0);
				break;
			}
		} else {
			this.tabBar.selectTab(-1);
		}
	}

	@Override
	public void setSelectedCharacter(CharacterId characterId) {
		if (characterId != null && characterId.getName() != null && !characterId.getName().isEmpty()) {
			this.selectedCharacter.setText(characterId.getName());
		} else {
			this.selectedCharacter.setText("Select character...");
		}
	}
	
	@Override
	public void setMessage(String textMessage) {
		if (textMessage != null && !textMessage.isEmpty()) {
			this.message.setText(textMessage);
			this.message.setVisible(true);
		} else {
			this.message.setText("");
			this.message.setVisible(false);
		}
	}
	
	@Override
	public void setPresenter(Presenter presenter) {
		this.presenter = presenter;
	}

	@Override
	public HasWidgets getCenterPanel() {
		return this.centerPanel;
	}

	@Override
	public Widget asWidget() {
		return this;
	}

	@UiHandler("signOutLink")
	public void onSignOutLinkClicked(ClickEvent event) {
		if (this.anonymous) {
			AnonymousLogin.logout();
		}
		Window.Location.assign(this.logoutURL);
	}
	
	@UiHandler("aboutLink")
	public void onAboutClicked(ClickEvent event) {
		AboutDialog aboutDialog = new AboutDialog();
		aboutDialog.show();
		aboutDialog.center();
	}

}
